﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.Samples.IHandlers;
using Microsoft.VisualStudio.IntelliTrace;
using System.Collections.Specialized;
using System.Globalization;
using System.Diagnostics;

namespace Microsoft.VisualStudio.Samples.ConsoleWriters
{
    public class TestChainConsoleWriter : ChainConsoleWriter, TestChainHandler
    {
        public void TestSessionEvent(IntelliTraceEvent ev)
        {
            TestSessionEvent testSessionEvent = ev as TestSessionEvent;
            Debug.Assert(testSessionEvent != null);

            NameValueCollection eventProperties = new NameValueCollection();
            eventProperties.Add("Ordinal", testSessionEvent.Ordinal.ToString(CultureInfo.CurrentCulture));
            eventProperties.Add("Event time", GetDateTimeFromEventTime(this.Process, testSessionEvent.EventTime).ToString("HH:mm:ss.fffffff", CultureInfo.CurrentCulture));
            eventProperties.Add("Session id", testSessionEvent.SessionId.ToString());

            DumpProperties(eventProperties);
        }

        public void TestCaseEndEvent(IntelliTraceEvent ev)
        {
            TestCaseEndEvent testCaseEndEvent = ev as TestCaseEndEvent;
            Debug.Assert(testCaseEndEvent != null);

            NameValueCollection eventProperties = GetTestCaseEventProperties(testCaseEndEvent);
            eventProperties.Add("Test case outcome", testCaseEndEvent.TestCaseOutcome.ToString(CultureInfo.CurrentCulture));

            DumpProperties(eventProperties);
        }

        public void TestCaseEvent(IntelliTraceEvent ev)
        {
            TestCaseEvent testCaseEvent = ev as TestCaseEvent;
            Debug.Assert(testCaseEvent != null);

            NameValueCollection eventProperties = GetTestCaseEventProperties(testCaseEvent);

            DumpProperties(eventProperties);
        }

        public void TestStepEvent(IntelliTraceEvent ev)
        {
            TestStepEvent testStepEvent = ev as TestStepEndEvent;
            Debug.Assert(testStepEvent != null);

            NameValueCollection eventProperties = new NameValueCollection();
            eventProperties.Add("Ordinal", testStepEvent.Ordinal.ToString(CultureInfo.CurrentCulture));
            eventProperties.Add("Event time", GetDateTimeFromEventTime(this.Process, testStepEvent.EventTime).ToString("HH:mm:ss.fffffff", CultureInfo.CurrentCulture));
            eventProperties.Add("Session id", testStepEvent.SessionId.ToString());
            eventProperties.Add("Test execution id", testStepEvent.TestExecutionId.ToString());
            eventProperties.Add("Test case id", testStepEvent.TestCaseId.ToString());
            eventProperties.Add("Test case manager id", testStepEvent.TestCaseManagerId.ToString(CultureInfo.CurrentCulture));
            eventProperties.Add("Test case name", testStepEvent.TestCaseName);
            eventProperties.Add("Is child test case", testStepEvent.IsChildTestCase.ToString());
            eventProperties.Add("Test step id", testStepEvent.TestStepId.ToString(CultureInfo.CurrentCulture));
            eventProperties.Add("Test step description", testStepEvent.TestStepDescription);

            DumpProperties(eventProperties);
        }

        private NameValueCollection GetTestCaseEventProperties(TestCaseEvent testCaseEvent)
        {
            NameValueCollection eventProperties = new NameValueCollection();
            eventProperties.Add("Ordinal", testCaseEvent.Ordinal.ToString(CultureInfo.CurrentCulture));
            eventProperties.Add("Event time", GetDateTimeFromEventTime(this.Process, testCaseEvent.EventTime).ToString("HH:mm:ss.fffffff", CultureInfo.CurrentCulture));
            eventProperties.Add("Session id", testCaseEvent.SessionId.ToString());
            eventProperties.Add("Test execution id", testCaseEvent.TestExecutionId.ToString());
            eventProperties.Add("Test case id", testCaseEvent.TestCaseId.ToString());
            eventProperties.Add("Test case manager id", testCaseEvent.TestCaseManagerId.ToString(CultureInfo.CurrentCulture));
            eventProperties.Add("Test case name", testCaseEvent.TestCaseName);
            eventProperties.Add("Is child test case", testCaseEvent.IsChildTestCase.ToString());
            return eventProperties;
        }
    }
}
